package city.spring.modules.medicalstation.drugsettings.drugsettings.controller;

import city.spring.modules.medicalstation.drugsettings.drugsettings.entity.DrugsettingsEntity;
import city.spring.modules.medicalstation.drugsettings.drugsettings.service.DrugsettingsService;
import city.spring.modules.medicalstation.drugsettings.drugsettings.vo.DrugsettingsVO;
import city.spring.utils.MyBatisUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
import com.baomidou.mybatisplus.extension.api.ApiController;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.lang.StringUtils;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.data.web.SortDefault;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * Controller：用药设置
 *
 * @author 谢金婷
 * @date 2020-10-15 09:52:06
 */
@RestController
@RequestMapping("/drugsettings")
public class DrugsettingsController extends ApiController {
    private final DrugsettingsService drugsettingsService;
    /**
     * 实体类可排序字段
     */
    private final List<SFunction<DrugsettingsEntity, ?>> entityOrderFields;

    public DrugsettingsController(DrugsettingsService drugsettingsService) {
        this.drugsettingsService = drugsettingsService;
        entityOrderFields = new ArrayList<>();
        entityOrderFields.add(DrugsettingsEntity::getId);
        entityOrderFields.add(DrugsettingsEntity::getOldmanId);
        entityOrderFields.add(DrugsettingsEntity::getDiseaseId);
        entityOrderFields.add(DrugsettingsEntity::getDiseaseQuantity);
        entityOrderFields.add(DrugsettingsEntity::getGmtCreate);
        entityOrderFields.add(DrugsettingsEntity::getGmtModified);
    }

    /**
     * 获取全部的 <strong>用药设置</strong> 列表
     */
    @GetMapping("all")
    public Object listAll(@PageableDefault Pageable pageable, DrugsettingsEntity entity) {
        LambdaQueryChainWrapper<DrugsettingsEntity> lambdaQuery = buildLambdaQuery(entity);
        MyBatisUtils.lambdaQueryAddOrder(lambdaQuery, pageable, entityOrderFields);
        return success(lambdaQuery.list());
    }

    /**
     * 分页获取 <strong>用药设置</strong> 列表
     *
     * @param pageable 分页参数信息
     */
    @GetMapping
    public Object list(@PageableDefault @SortDefault.SortDefaults(@SortDefault(sort = "id", direction = Sort.Direction.DESC)) Pageable pageable, DrugsettingsVO entity) {
//        LambdaQueryChainWrapper<DrugsettingsEntity> lambdaQuery = buildLambdaQuery(entity);
//        MyBatisUtils.lambdaQueryAddOrder(lambdaQuery, pageable, entityOrderFields);
//        Page<DrugsettingsEntity> page = lambdaQuery.page(MyBatisUtils.toPage(pageable, false));
//        return success(page);
        QueryWrapper<? extends DrugsettingsEntity> queryWrapper = buildQuery(entity);
        return success(drugsettingsService.listPage(MyBatisUtils.toPage(pageable), queryWrapper));
    }

    /**
     * 获取一个 <strong>用药设置</strong>
     *
     * @param id 主键ID
     */
    @GetMapping("{id}")
    public Object info(@PathVariable String id) {
        QueryWrapper<DrugsettingsVO> wrapper = new QueryWrapper<>();
        wrapper.eq("t1.id", id);
        List<DrugsettingsVO> records = drugsettingsService.listPage(new Page<>(), wrapper).getRecords();
        if (records.isEmpty()) {
            return success(null);
        }
        return success(records.get(0));
//        return success(drugsettingsService.getById(id));
    }

    /**
     * 添加一个 <strong>用药设置</strong>
     *
     * @param entity 修改后的信息
     */
    @PostMapping
    public Object add(@RequestBody DrugsettingsEntity entity) {
        drugsettingsService.saveDrugsettings(entity);

//        HttpHeaders headers = new HttpHeaders();
//        headers.add(HttpHeaders.LOCATION, String.valueOf(entity.getId()));
//        return new ResponseEntity<>(headers, HttpStatus.CREATED);
        return success(entity);
    }

    /**
     * 修改一个 <strong>用药设置</strong>
     *
     * @param id     主键ID
     * @param entity 修改后的信息
     */
    @PutMapping("{id}")
    public Object update(@PathVariable String id, @RequestBody DrugsettingsEntity entity) {
        entity.setId(id);
        drugsettingsService.updateDrugsettings(entity);
        return new ResponseEntity<>(HttpStatus.OK);
    }

    /**
     * 删除一个 <strong>用药设置</strong>
     *
     * @param id 主键ID
     */
    @DeleteMapping("{id}")
    public Object delete(@PathVariable String id) {
        drugsettingsService.deleteDrugsettings(id);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    /**
     * 删除多个 <strong>用药设置</strong>
     *
     * @param ids 主键ID列表
     */
    @DeleteMapping
    public Object deleteIds(@RequestBody List<String> ids) {
        drugsettingsService.deleteDrugsettings(ids);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    /**
     * 构建查询条件内容
     *
     * @param entity 实体对象
     * @return lambda query chain wrapper
     */
    private LambdaQueryChainWrapper<DrugsettingsEntity> buildLambdaQuery(DrugsettingsEntity entity) {
        LambdaQueryChainWrapper<DrugsettingsEntity> lambdaQuery = drugsettingsService.lambdaQuery();
        lambdaQuery.eq(StringUtils.isNotBlank(entity.getId()), DrugsettingsEntity::getId, entity.getId());
        lambdaQuery.like(StringUtils.isNotBlank(entity.getOldmanId()), DrugsettingsEntity::getOldmanId, entity.getOldmanId());
        lambdaQuery.like(StringUtils.isNotBlank(entity.getDiseaseId()), DrugsettingsEntity::getDiseaseId, entity.getDiseaseId());
        lambdaQuery.eq(entity.getDiseaseQuantity() != null, DrugsettingsEntity::getDiseaseQuantity, entity.getDiseaseQuantity());
        lambdaQuery.eq(entity.getGmtCreate() != null, DrugsettingsEntity::getGmtCreate, entity.getGmtCreate());
        lambdaQuery.eq(entity.getGmtModified() != null, DrugsettingsEntity::getGmtModified, entity.getGmtModified());
        return lambdaQuery;
    }

    /**
     * 构建查询条件内容
     *
     * @param entity 实体对象
     * @return lambda query chain wrapper
     */
    private QueryWrapper<DrugsettingsEntity> buildQuery(DrugsettingsVO entity) {
        QueryWrapper<DrugsettingsEntity> wrapper = new QueryWrapper<>();
        wrapper.eq(StringUtils.isNotBlank(entity.getId()), "t1.id", entity.getId());
        wrapper.like(StringUtils.isNotBlank(entity.getOldmanId()), "t1.oldman_id", entity.getOldmanId());
        wrapper.like(StringUtils.isNotBlank(entity.getOldmanName()), "t2.oldman_name", entity.getOldmanName());
        wrapper.like(StringUtils.isNotBlank(entity.getBedName()), "t3.name", entity.getBedName());
        wrapper.like(StringUtils.isNotBlank(entity.getDiseaseId()), "t1.disease_id", entity.getDiseaseId());
        wrapper.eq(entity.getDiseaseQuantity() != null, "t1.disease_quantity", entity.getDiseaseQuantity());
        return wrapper;
    }

}
